import uuid
from datetime import datetime
import random

from db import get_db

import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# from .. import logging_config  # 注意这里的路径

if __name__ == '__main__':
    with get_db() as db:
        logger.info(db)
        logger.info('db connection successful')

        # 查询
        query = "SELECT * FROM df_sn"
        db.execute(query)
        result = db.fetchall()
        for row in result:
            logger.info(row)

        # 插入
        # db.transaction = True
        insert_query = "INSERT INTO df_sn (sn, mac, create_time) VALUES (%s, %s, %s )"
        values = (random.randint(1000000000, 9999999999), uuid.uuid4().hex, datetime.now())
        logger.info(values)
        insert_result = db.execute(insert_query, values)
        logger.info(insert_result)
        # db._connection.commit()
        logger.info('insert successful')

        # 更新
        update_query = "UPDATE df_sn SET mac = %s, update_time = %s WHERE sn = %s"
        update_values = (uuid.uuid4().hex, datetime.now(), "1")
        update_result = db.execute(update_query, update_values)
        logger.info(update_result)
        logger.info('update successful')

        # 删除
        delete_query = "DELETE FROM df_sn WHERE id = %s";
        delete_values = (10,)
        delete_result = db.execute(delete_query, delete_values)
        logger.info(delete_result)

        db._connection.commit()         #注意：一定要提交

        logger.info('delete successful')
